package chapter1_3_wait_notify;

/**
 * @author huayuhao
 * @date 2020/12/15 16:02
 */
public class NotifyOrNotifyAll {

    // 创建共享变量
    static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(() -> {
            synchronized (object) {
                System.out.println("A 获得了锁");
                try {
                    System.out.println("让A进行阻塞");
                    object.wait();
                    System.out.println("A停止阻塞");
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        });
        Thread threadB = new Thread(() -> {
            synchronized (object) {
                System.out.println("B 获得了锁");
                try {
                    System.out.println("让B进行阻塞");
                    object.wait();
                    System.out.println("B停止阻塞");
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        });
        Thread threadC = new Thread(() -> {
            synchronized (object) {
                System.out.println("开始唤醒线程");
                // 唤醒其中一个
                object.notify();
                // 唤醒所有
                //object.notifyAll();
            }
        });

        threadA.start();
        threadB.start();

        Thread.sleep(1000);
        threadC.start();

        threadA.join();
        threadB.join();
        threadC.join();

        System.out.println("main over!!");
    }
}
